<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@include file="../head.jsp"%>
<html>
<head>
<script type="text/javascript">
(function(){
    Ext.onReady(function(){
        Ext.QuickTips.init();
        var key = '';
        var id = '';

        Ext.define('Airport', {
            extend: 'Ext.data.Model',
            fields: [
                {name:'id',type:'string'},
                {name:'name',type:'string'},
                {name:'ename',type:'string'},
                {name:'aircode',type:'string'},
                {name:'country',type:'string'},
                {name:'city',type:'string'},
                {name:'timeZone',type:'string'},
                {name:'belongRoute',type:'int'},
                {name:'belongRouteName',type:'string'},
                {name:'remark',type:'string'},
                {name:'creatorId',type:'string'},
                {name:'creatorName',type:'string'},
                {name:'createDate',type:'string'},
                {name:'version',type:'int'},
                {name:'status',type:'int'}
            ]
        });

        var queryName = Ext.create('Ext.form.field.Text',{
            id:'queryName',
            name:'queryName'
        });

        var store = Ext.create('Ext.data.Store', {
            model: 'Airport',
            pageSize:20,
            proxy: {
                type:'ajax',
                url:'/fsxt/base/airport!getAirport.action',
                reader: {
                    root: 'root',
                    totalProperty: 'totalProperty'
                }
            },
            autoLoad: true,
            listeners:{
                beforeload:function(){
                    store.proxy.extraParams = {
                        name:encodeURIComponent(queryName.getValue())
                    }
                }
            }
        });

        var grid = Ext.create('Ext.grid.Panel', {
            title: '机场三字代码维护',
            store: store,
            height: '100%',
            width: '100%',
            loadMask: true,
            autoScroll:true,
            columns:[
                Ext.create('Ext.grid.RowNumberer'),
                {text: "机场名称",dataIndex: 'name',sortable: true},
                {text: "英文名称",dataIndex: 'ename',sortable: true},
                {text: "三字代码",dataIndex: 'aircode',sortable: true,width:70},
                {text: "国家",dataIndex: 'country',sortable: true,width:70},
                {text: "城市",dataIndex: 'city',sortable: true,width:70},
                {text: "时区",dataIndex: 'timeZone',sortable: true,width:70},
                {text: "所属航线",dataIndex: 'belongRouteName',sortable: true},
                {text: "创建者",dataIndex: 'creatorName',sortable: true,width:70},
                {text: "创建时间",dataIndex: 'createDate',sortable: true},
                {text: "状态",dataIndex: 'status',sortable: true,width:70,
                    renderer:function(value){
                        if(value == 1)
                            return '<font color="blue">启用</font>';
                        return '<font color="red">冻结</font>';
                    }
                },
                {text: "备注",dataIndex: 'remark',sortable: false,width:300}
            ],
            tbar:Ext.create('Ext.toolbar.Toolbar',{
                items:['-',{
                    xtype:'label',
                    text:'机场名称:'
                },queryName,'-',{
                    xtype:'button',
                    iconCls:'find',
                    text:'查询',
                    handler:function(){
                        store.loadPage(1);
                    }
                },'-','->','-',{
                    xtype:'button',
                    iconCls:'create',
                    id:'createButton',
                    text:'新建',
                    handler:function(b,e){
                        key = 'create';
                        window.show(b);
                        form.getForm().reset();
                    }
                },'-',{
                    xtype:'button',
                    iconCls:'update',
                    text:'修改',
                    id:'updateButton',
                    handler:function(b,e){
                        key = 'update';
                        var selected = grid.getView().getSelectionModel().getSelection();
                        if(selected.length == 0){
                            Ext.Msg.show({title:'提示',msg:'请选择要修改记录',icon:Ext.Msg.WARNING,buttons:Ext.Msg.OK});
                            return;
                        }
                        var record = selected[0];
                        if(record.get('status') == 0){
                            Ext.Msg.show({title:'提示',msg:'记录已冻结,若要使用请先启用',icon:Ext.Msg.INFO,buttons:Ext.Msg.OK});
                            return;
                        }
                        id = record.get('id');
                        window.show(b);
                        form.getForm().loadRecord(record);
                        belongRoute.setValue(record.data.belongRoute+"");
                    }
                },'-',{
                    xtype:'button',
                    iconCls:'delete',
                    text:'冻结',
                    handler:function(){
                        var selected = grid.getView().getSelectionModel().getSelection();
                        if(selected.length == 0){
                            Ext.Msg.show({title:'提示',msg:'请选择要冻结记录',icon:Ext.Msg.WARNING,buttons:Ext.Msg.OK});
                            return;
                        }
                        var record = selected[0];
                        if(record.get('status') == 0){
                            Ext.Msg.show({title:'提示',msg:'记录为冻结状态',icon:Ext.Msg.INFO,buttons:Ext.Msg.OK});
                            return;
                        }
                        Ext.Msg.confirm('提示','确定冻结此部门?',function(btn){
                            if(btn == 'yes'){
                                var params = {id:record.get('id')};
                                var data = {};
                                data.params = params;
                                data.url = '/fsxt/base/airport!deleteAirport.action';
                                callAjax.call(data,callbackFunc);
                            }
                        });
                    }
                },'-',{
                    xtype:'button',
                    iconCls:'accept',
                    text:'启用',
                    handler:function(){
                        var selected = grid.getView().getSelectionModel().getSelection();
                        if(selected.length == 0){
                            Ext.Msg.show({title:'提示',msg:'请选择要启用记录',icon:Ext.Msg.WARNING,buttons:Ext.Msg.OK});
                            return;
                        }
                        var record = selected[0];
                        if(record.get('status') == 1){
                            Ext.Msg.show({title:'提示',msg:'记录为启用状态',icon:Ext.Msg.INFO,buttons:Ext.Msg.OK});
                            return;
                        }
                        Ext.Msg.confirm('提示','启用此部门?',function(btn){
                            if(btn == 'yes'){
                                var params = {id:record.get('id')};
                                var data = {};
                                data.params = params;
                                data.url = '/fsxt/base/airport!enableAirport.action';
                                callAjax.call(data,callbackFunc);
                            }
                        });
                    }
                },'-']
            }),
            bbar: Ext.create('Ext.PagingToolbar', {
                store: store,
                displayInfo: true,
                displayMsg: '当前第 {0} - {1} 条记录,共 {2} 条',
                emptyMsg: "没有机场记录"
            }),
            renderTo:Ext.getBody()
        });

        var belongRoute = Ext.create('Ext.form.ComboBox', {
            id:'belongRoute',
            name:'belongRoute',
            fieldLabel: '所属航线',
            queryMode: 'local',
            displayField: 'label',
            valueField: 'value',
            labelWidth:70,
            width:350,
            allowBlank:false,
            editable:false,
            store:Ext.create('Ext.data.Store', {
                model: 'commonModel',
                proxy: {
                    type:'ajax',
                    url:'/fsxt/sysBase!getBelongRoute.action'
                },
                autoLoad: true
            })
        });


        var form = Ext.create('Ext.form.Panel', {
            items: [{
                xtype:'textfield',
                fieldLabel: '机场名称',
                labelWidth:70,
                width:350,
                maxLength:200,
                id:'name',
                name: 'name',
                validator:notEmptyCheck
            },{
                xtype:'textfield',
                fieldLabel: '英文名称',
                labelWidth:70,
                width:350,
                maxLength:100,
                id:'ename',
                name: 'ename',
                validator:notEmptyCheck
            },{
                xtype:'textfield',
                fieldLabel: '三字代码',
                labelWidth:70,
                width:350,
                maxLength:10,
                id:'aircode',
                name: 'aircode',
                validator:notEmptyCheck
            },{
                xtype:'textfield',
                fieldLabel: '国家',
                labelWidth:70,
                width:350,
                maxLength:100,
                id:'country',
                name: 'country'
            },{
                xtype:'textfield',
                fieldLabel: '城市',
                labelWidth:70,
                width:350,
                maxLength:100,
                id:'city',
                name: 'city'
            },{
                xtype:'textfield',
                fieldLabel: '时区',
                labelWidth:70,
                width:350,
                maxLength:100,
                id:'timeZone',
                name: 'timeZone'

            },belongRoute,{
                xtype:'textarea',
                fieldLabel:'备注',
                labelWidth:70,
                maxLength:1000,
                width:350,
                id:'remark',
                name:'remark'
            }]
        });

        var callbackFunc = function(){
            store.loadPage(1);
        };

        var window = Ext.create('Ext.window.Window', {
            title: '机场信息',
            layout: 'fit',
            closeAction:'hide',
            resizable:false,
            width:400,
            height:350,
            plain: true,
            modal:true,
            draggable:true,
            items:form,
            buttons:[{
                text:'保存',
                handler:function(){
                    if(!form.getForm().isValid()){
                        Ext.Msg.show({title:'提示',msg:'请完整填写所需信息',icon:Ext.Msg.WARNING,buttons:Ext.Msg.OK});
                        return;
                    }

                    var post = form.getForm().getValues();
                    post.id = id;
                    var params = {};
                    params.data = encodeURIComponent(Ext.JSON.encode(post));
                    params.key = key;
                    params.id = id;
                    var data = {
                        url:'/fsxt/base/airport!saveAirport.action',
                        params:params
                    };
                    callAjax.call(data,callbackFunc);
                    window.hide();
                }
            },{
                text:'取消',
                handler:function(){
                    window.hide(Ext.get('createButton'));
                }
            }]
        });

    });
})();
</script>
</head>
<body>
</body>
</html>